package cn.teleus.common.util;

import cn.teleus.common.util.i18n.I18nMessage;
import cn.teleus.common.util.i18n.I18nService;
import cn.teleus.common.util.i18n.I18nType;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.ApplicationContext;

import java.util.LinkedHashMap;
import java.util.Map;

public class Root {
	private static I18nService i18nService;
	private Integer status = 0;
	private String msg = "";
	private Object data;
	private Object i18nMsg;
	
	public static String MSG_OK = "成功";
	public static String MSG_FAIL = "失败";
	
	public static int STATUS_OK = 1;
	public static int STATUS_FAIL = 0;

	public Integer getStatus() {
		return status;
	}
	public Root setStatus(Integer status) {
		this.status = status;
		return this;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Object getData() {
		return data;
	}
	public Root setData(Object data) {
		this.data = data;
		return this;
	}

	public Object getI18nMsg() {
		return i18nMsg;
	}

	public void setI18nMsg(Object i18nMsg) {
		this.i18nMsg = i18nMsg;
	}

	public String toJsonString(){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("msg", getMsg());
		jsonObject.put("status", getStatus());
		jsonObject.put("data", getData());
		jsonObject.put("i18nMsg",getI18nMsg());
		return jsonObject.toJSONString();
	}
	
	/**
	 * 简便的方法获取成功状态的ROOT
	 * @param msg 成功后的信息
	 */
	public static Root getRootOK(String msg){
		return getRoot(msg, STATUS_OK);
	}
	
	/**
	 * 默认成功状态的ROOT
	 */
	public static Root getRootOKAndSimpleMsg(){
		return getRoot(MSG_OK, STATUS_OK);
	}
	
	/**
	 * 简便的方法获取失败状态的ROOT
	 * @param msg 失败后的信息
	 */
	public static Root getRootFail(String msg){
		return getRoot(msg, STATUS_FAIL);
	}

	/**
	 * 简便的方法获取失败状态的ROOT
	 * @param msg 失败后的信息
	 * @param errorCode 错误代码
	 */
	public static Root getRootFail(String msg,Integer errorCode){
		return getRoot(msg, errorCode);
	}

	/**
	 * 简便的方法获取失败状态的ROOT
	 * @param errorCode 错误代码
	 */
	public static Root getRootFail(Integer errorCode){
		return getRootFail(MSG_FAIL,errorCode);
	}
	
	/**
	 * 默认失败状态的ROOT
	 */
	public static Root getRootFailAndSimpleMsg(){
		return getRoot(MSG_FAIL, STATUS_FAIL);
	}

	public static Root getRootFail(BaseErrorCodeEnum error) {
		return getRoot(error.getMsg(), error.getCode());
	}

	public static void setI18nService(I18nService i18nService) {
		Root.i18nService = i18nService;
	}

	protected static Root getRoot(String msg, Integer code) {
		Root root = new Root();
		root.setMsg(msg);
		//国际化
		String codeString = code.toString();
		if(i18nService != null){
			I18nMessage messages = i18nService.getMessages(codeString);
			if (messages != null){
				Map<I18nType, String> messageMap = messages.getMessageMap();
				LinkedHashMap<String,String> i18nMap = new LinkedHashMap<>();
				messageMap.forEach((i18nType, eachMsg) ->
						i18nMap.put(i18nType.getType(), eachMsg));
				root.setI18nMsg(i18nMap);
			}
		}
		root.setStatus(code);
		return root;
	}
}